package cz.cernilovsky.android.profileapp.services;

import java.io.IOException;
import java.util.List;

import org.json.JSONException;

import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.text.TextUtils;
import cz.cernilovsky.android.profileapp.R;
import cz.cernilovsky.android.profileapp.activities.MainActivity;
import cz.cernilovsky.android.profileapp.model.Location;
import cz.cernilovsky.android.profileapp.storage.LocationsStorage;

public class NotificationService {
	private static int id = 1;
	
	public static void notify(Context context, String message, String title) {
		Builder b = new NotificationCompat.Builder(context);
		Intent intent = new Intent(context, MainActivity.class);
		PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		b.setContentIntent(pi);
		b.setSmallIcon(R.drawable.ic_launcher);
		b.setContentTitle(title);
		b.setContentText(message);
		NotificationManager nm =
			    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		nm.notify(getUniqueId(), b.build());
	}
	
	public static void notify(Context context, int message, int title) {
		notify(context, context.getString(message), context.getString(title));
	}
	
	public static void notify(Context context, String message, int title) {
		notify(context, message, context.getString(title));
	}
	
	public static void notify(Context context, int message, String title) {
		notify(context, context.getString(message), title);
	}
	
	public static void notifySettingsIOError(Context context) {
		notify(context, R.string.error_IO_settings, R.string.error);
	}
	
	public static void notifySettingsJSONError(Context context) {
		notify(context, R.string.error_JSON_settings, R.string.error);
	}
	
	public static int getUniqueId() {
		return id++;
	}

	public static void mainNotification(Context context) {
			Builder b = new NotificationCompat.Builder(context);
			Intent intent = new Intent(context, MainActivity.class);
			PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
			b.setContentIntent(pi);
			b.setOngoing(true);
			b.setSmallIcon(R.drawable.ic_launcher);
			b.setContentTitle(context.getString(R.string.app_name));
			
			StringBuilder sb = new StringBuilder();
			sb.append(context.getString(R.string.monitoring_location));
			//List<String> loadedLocations = Preferences.getMonitoredLocations(context);
			try {
				Location[] loadedLocations = LocationsStorage.loadLocations(context);
				if(loadedLocations != null && loadedLocations.length > 0) {
					sb.append(": ");
					sb.append(TextUtils.join(", ", loadedLocations));
				}
				b.setContentText(sb.toString());
				
				NotificationManager nm =
					    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
				nm.notify(0, b.build());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
